﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Web;
using Framework.Common.Cache.ICacheImplement;

namespace Framework.Common.Cache
{
    public class CacheFactory
    {
        private CacheFactory()
        {
            try
            {
                string assembleDir = HttpContext.Current == null ? "/" : "~/bin";
                string assembleDirFullPath = PathHelper.MapPath(assembleDir);
                var dc = new DirectoryCatalog(assembleDirFullPath);
                var container = new CompositionContainer(dc);
                container.ComposeParts(this);
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message, ex);
                Cache = new InMemoryCache();
            }
        }

        [Import]
        public ICache Cache { get; set; }

        /// <summary>
        /// cache instance
        /// </summary>
        public static ICache CacheInstance
        {
            get { return Singleton<CacheFactory>.Instance.Cache; }
        }
    }
}